/*
 * Title: HotShop
 * Description: HotShop
 * Author: Alex Lee
 * Email: liyao20060101@gmail.com
 * Version: 1.0
 * Created on: 2010-9-15 上午10:55:07
 *
 * Copyright (c) 2010 HotShop.me
 */

package com.frame.common.db;

import java.io.Serializable;

/**
 * 
 * @author alex.lee at 2010-9-15 上午10:54:38
 *
 * desc：
 */
public class PageInfo  implements Serializable {

	private static final long serialVersionUID = 6080031235942359072L;

	private int dataCount = 0; 
	private int perPageSize = 0; 
	protected int page = 1; 
	private int startRecord = 0; 
	private int endRecord = 0; 
	private int pageCount = 0; 
	private int previous = 0; 
	private int next = 0; 
	private boolean hasPrevious = false; 
	private boolean hasNext = false;  
	private boolean isLimit = false;

	public void calculate() {
		
		if (perPageSize < 1 || dataCount < 0)
			return;

		if (dataCount == 0) {
			pageCount = 0;
		} else
			pageCount = (dataCount % perPageSize == 0) ? dataCount
					/ perPageSize : dataCount / perPageSize + 1;

		page = page < 1 ? 1 : page;
		page = page > pageCount ? pageCount : page;

		startRecord = perPageSize * (page - 1);
		startRecord = startRecord > dataCount ? dataCount : startRecord;

		endRecord = startRecord + perPageSize + 1;
		endRecord = endRecord > dataCount ? dataCount + 1 : endRecord;

		if (page > 1)
			this.hasPrevious = true;
		if (page < pageCount)
			this.hasNext = true;
	}

	public int getDataCount() {
		return dataCount;
	}

	public void setDataCount(int dataCount) {
		this.dataCount = dataCount;
	}

	public int getPerPageSize() {
		return perPageSize;
	}

	public void setPerPageSize(int pageSize) {
		this.perPageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int pageNow) {
		this.page = pageNow;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getEndRecord() {
		return endRecord;
	}

	public void setEndRecord(int dataEnd) {
		this.endRecord = dataEnd;
	}

	public int getStartRecord() {
		return startRecord;
	}

	public void setStartRecord(int dataStart) {
		this.startRecord = dataStart;
	}

	public boolean isHasPrevious() {
		return hasPrevious;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public int getPrevious() {
		if (page <= 1)
			previous = 1;
		else
			previous = page - 1;
		return previous;
	}

	public int getNext() {
		if (page >= pageCount)
			next = pageCount;
		else
			next = page + 1;
		return next;
	}

	public boolean isLimit() {
		return isLimit;
	}

	public void setLimit(boolean isLimit) {
		this.isLimit = isLimit;
	}
	
}
